<template>
  <div class="test-case-detail-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <h2>{{ testCase?.title || '测试用例详情' }}</h2>
            <div class="meta-info">
              <el-tag :color="getTestCaseStatusColor(testCase?.status || '')" effect="dark">
                {{ getTestCaseStatusLabel(testCase?.status || '') }}
              </el-tag>
              <el-tag :color="getTestCasePriorityColor(testCase?.priority || '')" effect="dark">
                {{ getTestCasePriorityLabel(testCase?.priority || '') }}
              </el-tag>
              <el-tag size="small">{{ getTestCaseTypeLabel(testCase?.type || '') }}</el-tag>
            </div>
          </div>
          <div class="header-right">
            <el-button @click="router.back()">返回</el-button>
            <el-button 
              type="primary" 
              @click="handleEdit"
              v-if="canEdit"
            >
              编辑
            </el-button>
          </div>
        </div>
      </template>
      
      <div v-loading="loading" class="detail-content">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="标题">{{ testCase?.title }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :color="getTestCaseStatusColor(testCase?.status || '')" effect="dark">
              {{ getTestCaseStatusLabel(testCase?.status || '') }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="类型">
            <el-tag>{{ getTestCaseTypeLabel(testCase?.type || '') }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="分类">
            <el-tag type="info">{{ getTestCaseCategoryLabel(testCase?.category || '') }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="优先级">
            <el-tag :color="getTestCasePriorityColor(testCase?.priority || '')" effect="dark">
              {{ getTestCasePriorityLabel(testCase?.priority || '') }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="负责人">
            {{ testCase?.assignee_name || '未分配' }}
          </el-descriptions-item>
          <el-descriptions-item label="执行次数">{{ testCase?.execution_count || 0 }}</el-descriptions-item>
          <el-descriptions-item label="通过率">
            {{ testCase?.pass_rate !== null && testCase?.pass_rate !== undefined 
              ? `${testCase.pass_rate.toFixed(1)}%` 
              : '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="最后执行结果" v-if="testCase?.last_execution_result">
            <el-tag :type="getExecutionResultType(testCase.last_execution_result)">
              {{ getExecutionResultLabel(testCase.last_execution_result) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="所属项目">{{ testCase?.project_name || '-' }}</el-descriptions-item>
          <el-descriptions-item label="描述" :span="2">
            <div v-html="testCase?.description || '-'"></div>
          </el-descriptions-item>
          <el-descriptions-item label="前置条件" :span="2">
            <div v-html="testCase?.preconditions || '-'"></div>
          </el-descriptions-item>
          <el-descriptions-item label="测试步骤" :span="2">
            <div v-if="testCase?.test_steps && testCase.test_steps.length > 0">
              <div 
                v-for="(step, index) in testCase.test_steps" 
                :key="index"
                class="test-step"
              >
                <div class="step-number">{{ step.step_number }}</div>
                <div class="step-content">
                  <div class="step-action"><strong>操作：</strong>{{ step.action }}</div>
                  <div class="step-expected"><strong>预期结果：</strong>{{ step.expected_result }}</div>
                </div>
              </div>
            </div>
            <span v-else>-</span>
          </el-descriptions-item>
          <el-descriptions-item label="预期结果" :span="2">
            <div v-html="testCase?.expected_result || '-'"></div>
          </el-descriptions-item>
          <el-descriptions-item label="后置条件" :span="2">
            <div v-html="testCase?.postconditions || '-'"></div>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ formatDateTime(testCase?.created_at) }}</el-descriptions-item>
          <el-descriptions-item label="更新时间">{{ formatDateTime(testCase?.updated_at) }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getTestCaseById, type TestCase } from '@/api/test-case'
import { usePermission } from '@/hooks/usePermission'
import { formatDateTime } from '@/utils/date'
import { 
  getTestCaseTypeLabel,
  getTestCaseCategoryLabel,
  getTestCaseStatusLabel,
  getTestCaseStatusColor,
  getTestCasePriorityLabel,
  getTestCasePriorityColor,
  getExecutionResultLabel,
  getExecutionResultType
} from '@/types/test-case'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const { hasPermission } = usePermission()
const userStore = useUserStore()

const loading = ref(false)
const testCase = ref<TestCase | null>(null)

const canEdit = computed(() => {
  if (!testCase.value) return false
  return hasPermission('test_case:update') || 
         hasPermission('*') ||
         testCase.value.creator_id === (userStore.user?.id || '') ||
         testCase.value.assignee_id === (userStore.user?.id || '')
})

const loadTestCase = async () => {
  const id = route.params.id as string
  if (!id) {
    ElMessage.error('测试用例ID不存在')
    router.back()
    return
  }
  
  loading.value = true
  try {
    const response = await getTestCaseById(id)
    testCase.value = response as any
  } catch (error: any) {
    ElMessage.error(error.message || '加载测试用例失败')
    router.back()
  } finally {
    loading.value = false
  }
}

const handleEdit = () => {
  router.push(`/test-cases/${route.params.id}/edit`)
}

onMounted(() => {
  loadTestCase()
})
</script>

<style scoped lang="scss">
.test-case-detail-container {
  padding: 20px;
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    
    .header-left {
      h2 {
        margin: 0 0 12px 0;
        font-size: 20px;
      }
      
      .meta-info {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
      }
    }
  }
  
  .detail-content {
    .test-step {
      display: flex;
      gap: 12px;
      margin-bottom: 16px;
      padding: 12px;
      background: #F5F7FA;
      border-radius: 4px;
      
      .step-number {
        flex-shrink: 0;
        width: 32px;
        height: 32px;
        line-height: 32px;
        text-align: center;
        background: #409EFF;
        color: white;
        border-radius: 50%;
        font-weight: 600;
      }
      
      .step-content {
        flex: 1;
        
        .step-action,
        .step-expected {
          margin-bottom: 8px;
          
          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }
  }
}
</style>

